package hanlen.mq;

import hanlen.mq.starter.configuration.MqAutoConfiguration;
import hanlen.mq.starter.configuration.RedissonConfiguration;
import hanlen.mq.starter.core.RedissonService;
import hanlen.mq.starter.listener.TestListener;
import hanlen.mq.starter.support.QueueUtil;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

/**
 * @program: purchaser-assist-service
 * @description
 * @author: wdye
 * @created: 2020-07-03 11:43
 */
@Slf4j
@SpringBootTest(classes = {RedissonConfiguration.class, MqAutoConfiguration.class})
public class RedissonGetter extends AbstractSpringContextTest {

    @Autowired
    private RedissonService redisService;

    /**
     * Mock 用户上下文 相关表测试数据
     */
    @BeforeEach
    void before() {

    }

    @Test
    void test() throws InterruptedException {
        final int threads = 1;
        final String[] names = {"张三", "李四", "王五", "赵柳", "戏中", "笑了", "钉钉", "Andy", "DingDang"};

        final String topicName = "testQueue";

        redisService.delete(topicName);
        redisService.delete(topicName + "_size");
        redisService.delete(topicName + "_dlq");
        redisService.delete(topicName + "_dlq_size");
        final long size = redisService.getAtomicLong(topicName + "_size").get();
        final long errSize = redisService.getAtomicLong(topicName + "_dlq_size").get();
        log.info("初始化 正常积压: {} 异常积压------------------>>>>>>>>>>> {}", size, errSize);
        Thread.sleep(2 * 1000);

        int total = 0;
        while (total < 20000) {
            total++;
            redisService.addQueue(topicName, QueueUtil.toJsonString(ParamData.builder().name("名称" + total).content("内容").build()));
            log.info("消息发送:{}次成功", total);
        }
    }

    @Test
    void test2() throws InterruptedException {
        final int threads = 1;
        final String[] names = {"张三", "李四", "王五", "赵柳", "戏中", "笑了", "钉钉", "Andy", "DingDang"};

        final String topicName = "testQueue";

        redisService.delete(topicName);
        redisService.delete(topicName + "_size");
        redisService.delete(topicName + "_dlq");
        redisService.delete(topicName + "_dlq_size");
        final long size = redisService.getAtomicLong(topicName + "_size").get();
        final long errSize = redisService.getAtomicLong(topicName + "_dlq_size").get();
        log.info("初始化 正常积压: {} 异常积压------------------>>>>>>>>>>> {}", size, errSize);
        Thread.sleep(2 * 1000);

        int total = 0;
        while (total < 100) {
            total++;
            redisService.addQueue(topicName, ParamData.builder().name("名称" + total).content("内容").build());
            log.info("消息发送:{}次成功", total);
            final long size1 = redisService.getAtomicLong(topicName + "_size").get();
            final long errSize1 = redisService.getAtomicLong(topicName + "_dlq_size").get();
            log.info("正常积压: {} 异常积压------------------>>>>>>>>>>> {}", size1, errSize1);
            try {
                Thread.sleep(2 * 1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
    }

}
